package com.yonder.controller;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author zxs
 * @Date 2021/3/23 20:09
 * Version 1.0
 */

import com.yonder.common.MessageConstant;
import com.yonder.entity.Result;
import com.yonder.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户名管理
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //获取当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            User user= (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        } catch (Exception e) {

            e.printStackTrace();

        }
        return new Result(false,MessageConstant.GET_USERNAME_FAIL);
    }

}
